package com.pinyoumei.app.activity.wallet;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Trace;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeSelectDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView tsdStartTxt,tsdStartTime,tsdEndTxt,tsdEndTime,tsdCancelBtn,tsdSubmitBtn;
    private DatePicker tsdPickView;
    private boolean isSelectDate = false;

    public TimeSelectDialog(Context context, int layoutResID, int[] listenedItems){
        super(context, R.style.dialogStyle);//这个决定dialog的样式
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Window window = getWindow();//获取一个窗口
        window.setGravity(Gravity.BOTTOM);//设置窗口的位置
        window.setWindowAnimations(R.style.bottom_menu_animation);//设置窗口进入时的动画特效
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取窗口的属性赋值给layoutParams
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);//设置窗口的属性
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.time_select_dialog_view);
        tsdStartTxt = findViewById(R.id.tsd_start_txt);
        tsdStartTime = findViewById(R.id.tsd_start_time);
        tsdEndTxt = findViewById(R.id.tsd_end_txt);
        tsdEndTime = findViewById(R.id.tsd_end_time);
        tsdPickView = findViewById(R.id.tsd_pick_view);
        tsdCancelBtn = findViewById(R.id.tsd_cancel_btn);
        tsdSubmitBtn = findViewById(R.id.tsd_submit_btn);
        tsdStartTime.setText(getDate());
        tsdEndTime.setText(getDate());
        tsdCancelBtn.setOnClickListener(this);
        tsdSubmitBtn.setOnClickListener(this);
        tsdStartTxt.setOnClickListener(this);
        tsdStartTime.setOnClickListener(this);
        tsdEndTxt.setOnClickListener(this);
        tsdEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tsd_cancel_btn:
                dismiss();
                break;
            case R.id.tsd_submit_btn:
                if(isSelectDate){
                    int y = tsdPickView.getYear();
                    int m = tsdPickView.getMonth()+1;
                    int d = tsdPickView.getDayOfMonth();
                    tsdEndTime.setText(y+"."+m+"."+d);
                    Utils.endTime = y+"-"+setMonthDay(m)+"-"+setMonthDay(d);
                    EventBusUtils.post("shuaxin");
                    dismiss();
                }else{
                    int y = tsdPickView.getYear();
                    int m = tsdPickView.getMonth()+1;
                    int d = tsdPickView.getDayOfMonth();
                    tsdStartTime.setText(y+"."+m+"."+d);
                    Utils.startTime = y+"-"+setMonthDay(m)+"-"+setMonthDay(d);
                    tsdStartTxt.setTextColor(context.getResources().getColor(R.color.color_999999));
                    tsdEndTxt.setTextColor(context.getResources().getColor(R.color.color_018d47));
                    isSelectDate = true;
                }
                break;
            case R.id.tsd_start_txt:
            case R.id.tsd_start_time:
                tsdStartTxt.setTextColor(context.getResources().getColor(R.color.color_018d47));
                tsdEndTxt.setTextColor(context.getResources().getColor(R.color.color_999999));
                isSelectDate = false;
                break;
            case R.id.tsd_end_txt:
            case R.id.tsd_end_time:
                tsdStartTxt.setTextColor(context.getResources().getColor(R.color.color_999999));
                tsdEndTxt.setTextColor(context.getResources().getColor(R.color.color_018d47));
                isSelectDate = true;
                break;
        }
    }

    @Override
    public void show(){
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

    private String getDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    private String setMonthDay(int index){
        String str = String.valueOf(index);
        if(index<=9){
            str = "0"+index;
        }

        return str;
    }

}
